package com.my.trace;

import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

/**
 * Created by vic on 11/3/13.
 */
public class Tracker implements LocationListener{
    final String TAG = "Tracker";
    //
    Activity superActivity;

    GoogleMap googleMap;
    AccountManager accountManager;
    String userName = "";
    MarkerOptions myMark;
    LatLng myLocate;
    Criteria crta;

    public interface LocationUpdateListener {
        public void updateLocation(Location location);
    }
    LocationUpdateListener updateListener;

    //
    public Tracker(Activity activity){
        superActivity = activity;
    }
    @Override
    public void onLocationChanged(Location location) {
        updateLocation(location);
    }
    @Override
    public void onProviderDisabled(String provider) {
        updateLocation(null);
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
    //
    public void addUpdateListener(LocationUpdateListener listener){
        updateListener = listener;
    }
    //
    public void setGoogleMap(GoogleMap map){
        googleMap = map;

        if(googleMap!= null){
            googleMap.setMyLocationEnabled(true);
            googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
            googleMap.setMyLocationEnabled(true);
            //map.getUiSettings().setCompassEnabled(true);
            //
            accountManager = AccountManager.get(superActivity);
            userName = accountManager.getAccountsByType("com.google")[0].name;

            myMark = new MarkerOptions();
            myMark.title(userName);
            //myMark.snippet("I'm Here.");
            //
            LocationManager locationManager = (LocationManager)superActivity.getSystemService(Context.LOCATION_SERVICE);
            crta = new Criteria();
            crta.setAccuracy(Criteria.ACCURACY_FINE);
            locationManager.requestLocationUpdates(locationManager.getBestProvider(crta, true), 2000, 0, this);
        }
    }
    //
    void updateLocation(Location location){
        if(location == null)  return;

        double lattitude = location.getLatitude();
        double longitude = location.getLongitude();

        myLocate = new LatLng(lattitude, longitude);
        myMark.position(myLocate);
        googleMap.clear();

        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myLocate, 30));
        googleMap.addMarker(myMark);
        //
        updateListener.updateLocation(location);
    }

}

